package org.ztr.yanai.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import org.ztr.yanai.blog.entity.Comment;
import org.ztr.yanai.blog.enums.CommentStatusEnum;
import org.ztr.yanai.blog.service.CommentService;
import org.ztr.yanai.blog.utils.Result;

import java.util.List;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 10:00
 * @Description: 评论管理控制器，提供评论CRUD操作
 * @Version: 1.0
 */
@RestController
@RequestMapping("/comment")
@Tag(name = "评论管理", description = "评论相关的 API 接口")
public class CommentController {

    private final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @PostMapping
    @Operation(summary = "创建评论", description = "根据传入的评论信息创建新评论")
    public Result<Comment> createComment(@RequestBody Comment comment) {
        boolean isSuccess = commentService.createComment(comment);
        return isSuccess ? Result.success(comment) : Result.error("创建失败");
    }

    @DeleteMapping("/{commentId}")
    @Operation(summary = "删除评论", description = "根据评论 ID 删除评论（逻辑删除）")
    @Parameter(name = "commentId", description = "评论 ID", required = true)
    public Result<?> deleteComment(@PathVariable Long commentId) {
        return commentService.deleteCommentById(commentId) ? Result.success("删除评论成功！") : Result.error("删除失败");
    }

    @GetMapping("/{commentId}")
    @Operation(summary = "获取评论详情", description = "根据评论 ID 获取评论详细信息")
    @Parameter(name = "commentId", description = "评论 ID", required = true)
    public Result<Comment> getCommentById(@PathVariable Long commentId) {
        return Result.success(commentService.getCommentById(commentId));
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询评论", description = "支持分页查询评论列表，可按文章ID或用户ID过滤")
    @Parameters({
            @Parameter(name = "pageNum", description = "当前页码，默认为 1", example = "1"),
            @Parameter(name = "pageSize", description = "每页显示条数，默认为 10", example = "10"),
            @Parameter(name = "postId", description = "文章 ID，可选", example = "1"),
            @Parameter(name = "userId", description = "用户 ID，可选", example = "1"),
            @Parameter(name = "status", description = "评论状态，可选", example = "APPROVED")
    })
    public Result<Page<Comment>> listCommentsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long postId,
            @RequestParam(required = false) Long userId,
            @RequestParam(required = false) CommentStatusEnum status) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        if (postId != null) {
            wrapper.eq("post_id", postId);
        }
        if (userId != null) {
            wrapper.eq("user_id", userId);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        wrapper.orderByDesc("created_at");
        return Result.success(commentService.listCommentsByPage(pageNum, pageSize, wrapper));
    }

    @PutMapping
    @Operation(summary = "更新评论", description = "根据传入的评论信息更新评论数据")
    public Result<?> updateComment(@RequestBody Comment comment) {
        return commentService.updateComment(comment) ? Result.success(comment) : Result.error("更新失败");
    }

    @PostMapping("/batch/status")
    @Operation(summary = "批量更新评论状态", description = "根据评论 ID 列表批量更新评论状态")
    @Parameters({
            @Parameter(name = "commentIds", description = "评论 ID 列表", required = true),
            @Parameter(name = "status", description = "目标状态", required = true)
    })
    public Result<?> batchUpdateStatus(
            @RequestBody List<Long> commentIds,
            @RequestParam CommentStatusEnum status) {
        return commentService.batchUpdateCommentStatus(commentIds, status) ? Result.success() : Result.error("批量更新失败");
    }
}
